package com.order.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.order.constant.MessageConstant;
import com.order.constant.StatusConstant;
import com.order.dto.ProductDTO;
import com.order.dto.ProductPageQueryDTO;
import com.order.entity.Product;
import com.order.entity.User;
import com.order.exception.DeletionNotAllowedException;
import com.order.mapper.ProductMapper;
import com.order.result.PageResult;
import com.order.service.ProductService;
import com.order.vo.ProductVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;


@Service
@Slf4j
public class ProduceServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;
    @Override
    public ProductVo getBydId(Long id) {

        return productMapper.getById(id);
    }

    @Override
    public PageResult PageQuery(ProductPageQueryDTO pageQueryDTO) {

        PageHelper.startPage(pageQueryDTO.getPage(),pageQueryDTO.getPageSize());

        Page<Product> page = productMapper.pageQuery(pageQueryDTO);

        long total = page.getTotal();
        List<Product> records = page.getResult();

        for (Product product : records) {
            if(product.getStatus() == 0){
                records.remove(product);
            }
        }

        return new PageResult(total,records);
    }

    @Override
    public PageResult PageQuery1(ProductPageQueryDTO pageQueryDTO) {

        PageHelper.startPage(pageQueryDTO.getPage(),pageQueryDTO.getPageSize());
        Page<Product> page = productMapper.pageQuery(pageQueryDTO);
        long total = page.getTotal();
        List<Product> records = page.getResult();
        return new PageResult(total,records);
    }

    @Override
    public void save(ProductDTO productDTO) {
        Product product =new Product();
        BeanUtils.copyProperties(productDTO,product);
        //向乐器表插入1条数据
        productMapper.insert(product);
    }

    /**
     * 乐器批量删除
     * @param ids
     */
    @Transactional
    public void deleteProduct(List<Long> ids) {

        //判断当前菜品是否能够删除（是否起售中）
        for(Long id : ids){
            Product product =  productMapper.getById1(id);
            if (product.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //根据ID集合批量删除数据
        productMapper.deleteByIds(ids);


    }

    @Override
    public void startOrStop(Integer status, Long id) {
         Product product = Product.builder()
                .id(id)
                .status(status)
                .build();
        productMapper.update(product);
    }

    @Override
    public void updateProduct(ProductDTO productDTO) {
        //修改基本信息
        Product product = new Product();
        BeanUtils.copyProperties(productDTO,product);
        productMapper.update(product);
    }
}
